﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using IDesktop.Core;

namespace IDesktop.View {
    /// <summary>
    /// WeatherControl.xaml 的交互逻辑
    /// </summary>
    public partial class WeatherControl : UserControl {
        private static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(WeatherControl), new UIPropertyMetadata(null, ImageSourceChanged));
        private static readonly DependencyProperty FutureWeatherInfoProperty = DependencyProperty.Register("FutureWeatherInfo", typeof(FutureWeatherInfo), typeof(WeatherControl), new UIPropertyMetadata(null, TemperatureChanged));

        public BitmapImage ImageSource {
            get {
                return (BitmapImage)this.GetValue(ImageSourceProperty);
            }
            set {
                if (value != null) {
                    this.SetValue(ImageSourceProperty, value);
                }
            }
        }

        public FutureWeatherInfo FutureWeatherInfo {
            get {
                return (FutureWeatherInfo)this.GetValue(FutureWeatherInfoProperty);
            }
            set {
                if (value != null) {
                    this.SetValue(FutureWeatherInfoProperty, value);
                }
            }
        }

        public WeatherControl() {
            InitializeComponent();
        }

        private static void ImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            ((WeatherControl)d).weatherPic.Source = (BitmapImage)e.NewValue;
        }

        private void WeatherPic_SourceUpdated(object sender, DataTransferEventArgs e) {
            Storyboard storyBoard = (Storyboard)this.FindResource("storyBoard");
            storyBoard.Begin();
        }

        private static void TemperatureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            ((WeatherControl)d).temperature.Text = (string)e.NewValue;
            ((WeatherControl)d).temperature.Visibility = Visibility.Visible;
        }
    }
}
